/**
 * 
 */
package org.gigi.security.web;

import java.util.List;

import org.gigi.core.web.BasicResponse;
import org.gigi.core.web.BeanResponse;
import org.gigi.core.web.ListResponse;
import org.gigi.security.domain.Menu;
import org.gigi.security.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 菜单控制层代码。
 *
 * @author Eugene Wang 2011-9-12
 * @see
 */
@Controller
@RequestMapping("/sec/menu")
public class MenuController {
	/*Logger*/
	private final Logger logger = LoggerFactory.getLogger(getClass());
	private final boolean debug = logger.isDebugEnabled();

	@Autowired
	private MenuService menuService;

	/**
	 * 获取直接子菜单列表。
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public @ResponseBody ListResponse<Menu> findChildren(@RequestParam("node") String code) {
		if (debug) {
			logger.debug("获取子菜单。");
		}
		List<Menu> menus = menuService.findChildren(code);
		ListResponse<Menu> response = new ListResponse<Menu>("成功获取所有菜单", menus);
		return response;
	}

	/**
	 * 新创建一个菜单。
	 * @param menu
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public @ResponseBody BeanResponse<Menu> create(@RequestBody Menu menu) {
		menuService.create(menu);
		BeanResponse<Menu> response = new BeanResponse<Menu>("成功创建菜单", menu);
		return response;
	}

	/**
	 * 根据code删除一个菜单。
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{code}", method=RequestMethod.DELETE)
	public @ResponseBody BasicResponse remove(@PathVariable("code") String code) {
		menuService.delete(code);
		return new BasicResponse("成功删除菜单");
	}

	/**
	 * 修改一个菜单。
	 * @param menu
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public @ResponseBody BeanResponse<Menu> update(@RequestBody Menu menu) {
		menuService.update(menu);
		BeanResponse<Menu> response = new BeanResponse<Menu>("成功修改菜单", menu);
		return response;
	}
	/**
	 * 读取一个菜单
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/{code}",  method=RequestMethod.GET)
	public @ResponseBody BeanResponse<Menu> get(@PathVariable("code") String code) {
		Menu menu = menuService.findByCode(code);
		BeanResponse<Menu> response = new BeanResponse<Menu>();
		response.setData(menu);
		return response;
	}
	/**
	 * 获取所有菜单，并返回顶级菜单列表。
	 * @return
	 */
	@RequestMapping(value = "/all", method=RequestMethod.GET)
	public @ResponseBody ListResponse<Menu> findAll() {
		if (debug) {
			logger.debug("获取所有菜单。");
		}
		List<Menu> menus = menuService.findAll();
		ListResponse<Menu> response = new ListResponse<Menu>("成功获取所有菜单", menus);
		return response;
	}
}
